﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace mstdump_Analyzer
{
    public partial class Settings : Form
    {
        /// <summary>
        /// constructor + initializing the panel.
        /// </summary>
        public Settings()
        {
            InitializeComponent();
            init();
        }
        //
        //----------------------------------------------------
        /// <summary>
        /// init the settings panel. get the latest saved settings and display them.
        /// </summary>
        private void init()
        {
            // add items from the settings file
            try
            {
                keywordsCheckedListBox.Items.Clear();
                Properties.Settings.Default.Reload();
                for (int i = 0; i < Properties.Settings.Default.messagesKeywords.Count; i++)
                {
                    keywordsCheckedListBox.Items.Add(Properties.Settings.Default.messagesKeywords[i]);
                }
                //
                dmesgKeywordsCheckedListBox.Items.Clear();
                for (int i = 0; i < Properties.Settings.Default.dmesgKeywords.Count; i++)
                {
                    dmesgKeywordsCheckedListBox.Items.Add(Properties.Settings.Default.dmesgKeywords[i]);
                }
                //
                printGapTextBox.Text = Properties.Settings.Default.logsIdleThresholdSec.ToString();
            }
            catch
            { }
        }
        //
        //----------------------------------------------------
        //
        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// display the text horizontally for a tabpage
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void settingsTabControl_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {
                Graphics g;
                string text;
                int x, y;
                SizeF textSize;
                TabControl ctlTab = (TabControl)sender;
                g = e.Graphics;
                text = ctlTab.TabPages[e.Index].Text;
                textSize = g.MeasureString(text, ctlTab.Font);
                x = e.Bounds.Left + 6;
                y = (int)(e.Bounds.Top + (e.Bounds.Height - textSize.Height) / 2);
                g.DrawString(text, ctlTab.Font, Brushes.Black, x, y);
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// add a keyword to messagesKeywords
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!keywordsCheckedListBox.Items.Contains(keywordTextBox.Text))
                {
                    keywordsCheckedListBox.Items.Add(keywordTextBox.Text);
                    Properties.Settings.Default.messagesKeywords.Add(keywordTextBox.Text);
                    Properties.Settings.Default.Save();
                }
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// remove a keyword from messagesKeywords
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void removeSelectedButton_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = keywordsCheckedListBox.Items.Count - 1; i >= 0; i--)
                {
                    if (keywordsCheckedListBox.GetItemCheckState(i) == CheckState.Checked)
                    {
                        keywordsCheckedListBox.Items.RemoveAt(i);
                        Properties.Settings.Default.messagesKeywords.RemoveAt(i);
                    }
                }
                Properties.Settings.Default.Save();
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// remove all messagesKeywords
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void removeAllButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("All keywords will be removed!\nAre you sure you want to continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // clear the list box
                    keywordsCheckedListBox.Items.Clear();
                    // clear the settings file 
                    Properties.Settings.Default.messagesKeywords.Clear();
                    Properties.Settings.Default.Save();
                }
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// reset messagesKeywords to default
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void resetToDefaultButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Default keywords will be loaded.\nAre you sure you want to continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // clear
                    keywordsCheckedListBox.Items.Clear();
                    Properties.Settings.Default.messagesKeywords.Clear();
                    // add default values
                    string[] messagesKeywords = new string[] { ".ERR", "PLB", "crash", "go bit not cleared", "timed out", "Linux", "updt_role", "HERE" };
                    for (int i = 0; i < messagesKeywords.Length; i++)
                    {
                        Properties.Settings.Default.messagesKeywords.Add(messagesKeywords[i]);
                        keywordsCheckedListBox.Items.Add(messagesKeywords[i]);
                    }
                    // save changes
                    Properties.Settings.Default.Save();
                }
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// import settings from a file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void importButton_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "mlnxconfig|*.mlnxconfig";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (ofd.FileName != "")
                    {
                        SettingsIO.Import(ofd.FileName);
                        init();
                        MessageBox.Show("The settings were loaded.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }   
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// export settings to a file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exportButton_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "mlnxconfig|*.mlnxconfig";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    if (sfd.FileName != "")
                    {
                        SettingsIO.Export(sfd.FileName);
                        MessageBox.Show("The settings were saved.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// reset all settings to default
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void resetAllToDefaultButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Default settings will be loaded.\nAre you sure you want to continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    
                    Properties.Settings.Default.Reset();
                    init();
                    MessageBox.Show("Default settings were restored.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// add keyword to dmesgKeywords
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dmesgAddButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (!dmesgKeywordsCheckedListBox.Items.Contains(dmesgKeywordTextBox.Text))
                {
                    dmesgKeywordsCheckedListBox.Items.Add(dmesgKeywordTextBox.Text);
                    Properties.Settings.Default.dmesgKeywords.Add(dmesgKeywordTextBox.Text);
                    Properties.Settings.Default.Save();
                }
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// remove keywords from dmesgKeywords
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dmesgRemoveSelectedButton_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = dmesgKeywordsCheckedListBox.Items.Count - 1; i >= 0; i--)
                {
                    if (dmesgKeywordsCheckedListBox.GetItemCheckState(i) == CheckState.Checked)
                    {
                        dmesgKeywordsCheckedListBox.Items.RemoveAt(i);
                        Properties.Settings.Default.dmesgKeywords.RemoveAt(i);
                    }
                }
                Properties.Settings.Default.Save();
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// remove all keyworks from dmesgKeywords
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dmesgRemoveAllButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("All keywords will be removed!\nAre you sure you want to continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // clear the list box
                    dmesgKeywordsCheckedListBox.Items.Clear();
                    // clear the settings file 
                    Properties.Settings.Default.dmesgKeywords.Clear();
                    Properties.Settings.Default.Save();
                }
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// reset dmesgKeywords to default
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dmesgResetToDfaultButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Default keywords will be loaded.\nAre you sure you want to continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // clear
                    dmesgKeywordsCheckedListBox.Items.Clear();
                    Properties.Settings.Default.dmesgKeywords.Clear();
                    // add default values
                    string[] dmesgKeywords = new string[] { "PLB", "not found", "error", "Call Trace"};
                    for (int i = 0; i < dmesgKeywords.Length; i++)
                    {
                        Properties.Settings.Default.dmesgKeywords.Add(dmesgKeywords[i]);
                        dmesgKeywordsCheckedListBox.Items.Add(dmesgKeywords[i]);
                    }
                    // save changes
                    Properties.Settings.Default.Save();
                }
            }
            catch { }
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// reset to default the threshold for the gap size check in the messages file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printGapResetToDefaulButton_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.logsIdleThresholdSec = 10;
            printGapTextBox.Text = "10";
            // save changes
            Properties.Settings.Default.Save();
        }
        //
        //----------------------------------------------------
        
        /// <summary>
        /// set the threshold for the gap size check in the messages file.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printGapTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (!printGapTextBox.Text.Equals(""))
                {
                    int threshold = Int32.Parse(printGapTextBox.Text);
                    Properties.Settings.Default.logsIdleThresholdSec = threshold;
                    // save changes
                    Properties.Settings.Default.Save();
                }
            }
            catch
            {
                MessageBox.Show("The time must be an interger number", "Illegal input", MessageBoxButtons.OK, MessageBoxIcon.Error);
                printGapResetToDefaulButton_Click(null, null);
            }
        }
        //
        //----------------------------------------------------
        //
    }
}
